﻿//4、事件event
//1、event修饰的委托，只能够被类内调用执行，类外不可调用执行的
//2、event修饰的委托，类外不可直接赋值，只能够通过+、-增减其中蕴含的方法

//委托方法，需要携带的参数太多了，参数还会经常变动,所以将所有参数都包含到一个class里面

namespace ConsoleApp4
{
    class Player
    {
        public event EventHandler? OnAttack = null;

        public void DoAOE()
        {
            if(OnAttack != null)
            {
                OnAttack(this, EventArgs.Empty);
            }
        }
    }

    class Enemy
    {
        public void AttackMe(object? sender,EventArgs e)
        {
            Console.WriteLine( "Enemy 被攻击了");
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            DelegateExample delegateExample = new DelegateExample();
            delegateExample.DoFunction();
            Console.WriteLine("\n************");

            Player player = new Player();
            Enemy enemy = new Enemy();

            player.OnAttack += enemy.AttackMe;
            player.DoAOE();
        }
    }




    class DelegateExample
    {
        public delegate void DoDelegate(object sender, EventArgs e);
        public event DoDelegate DoEvent;
        internal void DoFunction()
        {
            //声明一个委托变量，并将已知方法作为构造函数的参数
            DoDelegate doDelegate = new DoDelegate(Test);
            object sender = 888;
            EventArgs e = new EventArgs();
            DoEvent += new DoDelegate(doDelegate);
            DoEvent(sender, e);
        }
        void Test(object sender, EventArgs e)
        {
            Console.Write($"valueStr={sender}");
        }
    }
}
